package com.zzh.interview.controller;

import com.zzh.interview.common.BaseResponse;
import com.zzh.interview.common.ResultUtils;
import com.zzh.interview.service.EmailService;
import com.zzh.interview.utils.CodeUtils;
import com.zzh.interview.utils.EmailRightUtils;
import javafx.scene.layout.Background;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 张 志 豪
 * @version 1.0
 */
@RestController
@RequestMapping("/email")
//@CrossOrigin(origins  = "*", allowCredentials = "true")
public class EmailController {

    @Autowired
    private EmailService emailService;

    //请求邮箱
    @GetMapping("/sendCode")
    public BaseResponse sendEmailCode(@RequestParam String toEmail){
        if (StringUtils.isBlank(toEmail)){
            return ResultUtils.error(40001,"邮箱不能为空");
        }

        //判断邮箱是否正确
        boolean rightEmail = EmailRightUtils.isRightEmail(toEmail);
        if (!rightEmail){
            ResultUtils.error(40001,"邮箱不正确不合规");
        }

        //生成一个6位的随机码
        String code = CodeUtils.getCodeToSix();
        String subject = "码猴网注册验证码";
        boolean isSuccess = emailService.sendMsg(toEmail, subject, code.toString());
        if (isSuccess){
            System.out.println(subject + code);
            return ResultUtils.success("验证码已发送至邮箱");
        }else {
            return ResultUtils.error(40001,"邮箱不正确不合规");
        }
    }

}
